package com.dao.impl;

import com.dao.RoleDao;
import com.dao.UserDao;
import com.pojo.User;
import com.utils.MyJdbcTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

@Repository
public class UserDaoImpl implements UserDao {
    @Autowired
    private MyJdbcTemplate jdbcTemplate;

    @Override
    public User Login(String name, String pwd) {
        String sql = "select * from smbms_user where userCode=? and userPassword=?";
        return jdbcTemplate.queryForObject(sql, new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                User user = new User();
                user.setId(rs.getLong("id"));
                user.setUserCode(rs.getString("userCode"));
                user.setUserName(rs.getString("userName"));
                user.setUserPassword(rs.getString("userPassword"));
                user.setGender(rs.getInt("gender"));
                user.setBirthday(rs.getDate("birthday"));
                user.setPhone(rs.getString("phone"));
                user.setAddress(rs.getString("address"));
                user.setUserRole(roleDao.findRoleById((int) rs.getLong("userRole")));
                return user;
            }
        }, name, pwd);
    }

    @Override
    public List<User> searchUser(String username) {
        String sql = "select * from smbms_user where userName REGEXP ?";
        return jdbcTemplate.query(sql, new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                User user = new User();
                user.setId(rs.getLong("id"));
                user.setUserCode(rs.getString("userCode"));
                user.setUserName(rs.getString("userName"));
                user.setUserPassword(rs.getString("userPassword"));
                user.setGender(rs.getInt("gender"));
                user.setBirthday(rs.getDate("birthday"));
                user.setPhone(rs.getString("phone"));
                user.setAddress(rs.getString("address"));
                user.setUserRole(roleDao.findRoleById((int) rs.getLong("userRole")));
                return user;
            }
        },username);
    }

    @Autowired
    private RoleDao roleDao;

    public int addUser(User user) {
        String sql = "insert into " +
                "smbms_user(userCode,userName,userPassword,gender,birthday," +
                "phone,address,userRole,createdBy,creationDate)" +
                " values(?,?,?,?,?,?,?,?,?,?)";
        return jdbcTemplate.update(sql,user.getUserCode(),user.getUserName(),user.getUserPassword(),user.getGender(),
                user.getBirthday(),user.getPhone(),user.getAddress(),user.getUserRole().getId(),user.getCreatedBy(),user.getCreationDate());
    }

    public int deleteUser(int id) {
        String sql = "delete from smbms_user where id=?";
        return jdbcTemplate.update(sql,id);
    }

    public int updataUser(User user) {
        String sql = "update smbms_user set userName = ?,gender=?,birthday=?,phone=?,address=?,userRole=?,modifyBy=?,modifyDate=? where id=?";
        return jdbcTemplate.update(sql,user.getUserName(),user.getGender(),user.getBirthday(),user.getPhone(),user.getAddress(),user.getUserRole().getId(),1,new Date(),user.getId());

    }

    public User findUserById(int id) {
        String sql = "select * from smbms_user where id=?";
        return jdbcTemplate.queryForObject(sql, new RowMapper<User>() {
            public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                User user = new User();
                user.setId(rs.getLong("id"));
                user.setUserCode(rs.getString("userCode"));
                user.setUserName(rs.getString("userName"));
                user.setUserPassword(rs.getString("userPassword"));
                user.setGender(rs.getInt("gender"));
                user.setBirthday(rs.getDate("birthday"));
                user.setPhone(rs.getString("phone"));
                user.setAddress(rs.getString("address"));
                user.setUserRole(roleDao.findRoleById((int) rs.getLong("userRole")));
                user.setCreatedBy(rs.getLong("createdBy"));
                user.setCreationDate(rs.getDate("creationDate"));
                user.setModifyBy(rs.getLong("modifyBy"));
                user.setModifyDate(rs.getDate("modifyDate"));
                return user;
            }
        },id);
    }

    public List<User> findAll() {
        String sql = "select * from smbms_user";
        return jdbcTemplate.query(sql, new RowMapper<User>() {
            public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                User user = new User();
                user.setId(rs.getLong("id"));
                user.setUserCode(rs.getString("userCode"));
                user.setUserName(rs.getString("userName"));
                user.setUserPassword(rs.getString("userPassword"));
                user.setGender(rs.getInt("gender"));
                user.setBirthday(rs.getDate("birthday"));
                user.setPhone(rs.getString("phone"));
                user.setAddress(rs.getString("address"));
                user.setUserRole(roleDao.findRoleById((int) rs.getLong("userRole")));
                user.setCreatedBy(rs.getLong("createdBy"));
                user.setCreationDate(rs.getDate("creationDate"));
                user.setModifyBy(rs.getLong("modifyBy"));
                user.setModifyDate(rs.getDate("modifyDate"));
                return user;
            }
        });
    }

    @Override
    public String findPasswordById(int id) {
        String sql = "select userPassword from smbms_user where id = ?";
        return jdbcTemplate.queryForObject(sql,String.class,id);
    }

    @Override
    public int updatePassword(int id, String pwd) {
        String sql = "update smbms_user set userPassword = ? where id =?";
        return jdbcTemplate.update(sql,pwd,id);
    }
}
